<template>
<div class="col-md-12">
  <rd-widget>
    <rd-widget-header :header="header">
    </rd-widget-header>
    <rd-widget-body>
      <my-file-upload 
        :storage-api="storageApi" 
        :max-size="maxSize"  
        accept=".tar" 
        upload-type="webapp" 
        :success="onSuccess" 
        :error="onError"></my-file-upload>
    </rd-widget-body>
  <rd-widget>
</div>
</template>

<script>
import toast from 'libs/toast.js'
import settings from 'share/server/settings.js'
import msgbox from 'libs/msgbox.js'
import {storageApi, commonApi} from 'share/server/api.js'
import locales from '../locales/webAppUpload.js'

export default {
  locales,
  data () {
    return {
      header: {
        icon: 'fa fa-file',
        title: this.$t("title")
      },
      maxSize: 10*1024*1024, 
      storageApi: storageApi
    }
  },
  methods: {
    onSuccess (res) {
      if (res.code !== 0) {
        toast.code(this, res.data)
        return
      }

      msgbox.alert({
        title: this.$t('alert.uploadSuccess'),
        text: this.$t('alert.needRestartService') + this.$t('alert.restartNow'),
        type: "success",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        cancelButtonText: this.$t('common.cancel'),
        confirmButtonText: this.$t('common.confirm'),
        closeOnConfirm: false,
        showLoaderOnConfirm: true    
      }, 
      function() {
        commonApi.sendCmd('restartService') 
        setTimeout(function() {
          msgbox.close()
          location.reload(true)
        }, settings.restartWaitTime)
      }.bind(this))      
    },
    onError (reason) {
      if (reason) {
        toast.e(this, this.$t('common.fail'), reason)
      } else {
        toast.e(this, this.$t('common.fail'), this.$t('error.unknown'))
      }
    }
  }
}
</script>

<style>
</style>